/**
 * Created by Demon-pro on 2015/6/28.
 */
var Choose = function () {
    var saveValidate = function () {
        if ($("#myglState").is(':checked')) {
            var E3 = $("#E3").val();
            if (E3 == "") {
                toast.error("炉膛漏风率 不能为空")
                return false;
            }
            var E4 = $("#E4").val();
            if (E4 == "") {
                toast.error("炉膛出口过量空气系数 不能为空")
                return false;
            }
            var E5 = $("#E5").val();
            if (E5 == "") {
                toast.error("机械未完全燃烧损失 不能为空")
                return false;
            }
            var E6 = $("#E6").val();
            if (E6 == "") {
                toast.error("锅炉燃煤量 不能为空")
                return false;
            }
            var E54 = $("#E54").val();
            if (E54 == "") {
                toast.error("锅炉一次风率 不能为空")
                return false;
            }
            var E67 = $("#E67").val();
            if (E67 == "") {
                toast.error("冷风温度 不能为空")
                return false;
            }
            var E87 = $("#E87").val();
            if (E87 == "") {
                toast.error("热风温度 不能为空")
                return false;
            }
        }
        var E8 = $("#E8").val();
        if (E8 == "") {
            toast.error("磨煤机总台数 不能为空")
            return false;
        }

        var E9 = $("#E9").val();
        if (E9 == "") {
            toast.error("备用磨煤机台数 不能为空")
            return false;
        }

        var E2 = $("#E2").val();
        if (E2 == "") {
            toast.error("均匀性指数 不能为空")
            return false;
        }

        if ($("#E12State").is(':checked')) {
            var E12 = $("#E12").val();
            if (E12 == "") {
                toast.error("煤粉细度R90 不能为空")
                return false;
            }
        }

        var E61 = $("#E61").val();
        if (E61 == "") {
            toast.error("漏风系数 不能为空")
            return false;
        }

        if ($("#E74State").is(':checked')) {
            var E74 = $("#E74").val();
            if (E74 == "") {
                toast.error("磨煤机出口温度 不能为空")
                return false;
            }
        }

        if ($("#E75State").is(':checked')) {
            var E75 = $("#E75").val();
            if (E75 == "") {
                toast.error("煤粉水分 不能为空")
                return false;
            }
        }

        return true;
    }

    var getChoose = function () {
        var operateFiled =
        {
            myglState: $("#myglState").is(':checked') ? "1" : "0",
            e12State: $("#E12State").is(':checked') ? "1" : "0",
            e74State: $("#E74State").is(':checked') ? "1" : "0",
            e75State: $("#E75State").is(':checked') ? "1" : "0"
        }
        return{
            E3: operateFiled.myglState == '1' ? $("#E3").val() : 0,
            E4: operateFiled.myglState == '1' ? $("#E4").val() : 0,
            E6: operateFiled.myglState == '1' ? $("#E6").val() : 0,
            E5: operateFiled.myglState == '1' ? $("#E5").val() : 0,
            E54: operateFiled.myglState == '1' ? $("#E54").val() : 0,
            E67: operateFiled.myglState == '1' ? $("#E67").val() : 0,
            E87: operateFiled.myglState == '1' ? $("#E87").val() : 0,
            E12: operateFiled.e12State == '1' ? $("#E12").val() : 0,
            E74: operateFiled.e74State == '1' ? $("#E74").val() : 0,
            E75: operateFiled.e75State == '1' ? $("#E75").val() : 0,
            E7: $("#E7").val(),
            E8: $("#E8").val(),
            E9: $("#E9").val(),
            E2: $("#E2").val(),
            splitterType: $("#splitter_type").val(),
            E61: $("#E61").val(),
            myglState: $("#myglState").is(':checked') ? "1" : "0",
            e12State: $("#E12State").is(':checked') ? "1" : "0",
            e74State: $("#E74State").is(':checked') ? "1" : "0",
            e75State: $("#E75State").is(':checked') ? "1" : "0",
            caseId: $("#caseId").val()
        }
    }

    function popoverHandle() {
        var data = [
            {
                name: "钢球磨煤机",
                data: [
                    {
                        type: "贮仓式",
                        K: "0.2~0.4*"
                    },
                    {
                        type: "直吹式",
                        K: "0.25"
                    }
                ]
            },
            {
                name: "中速磨煤机",
                data: [
                    {
                        type: "负压",
                        K: "0.2"
                    }
                ]
            },
            {
                name: "风扇磨煤机",
                data: [
                    {
                        type: "不带烟气下降管",
                        K: "0.2"
                    },
                    {
                        type: "带烟气下降管",
                        K: "0.3"
                    }
                ]
            }
        ];

        function getTable() {
            var $table = $("<table>").css("width", "600px").addClass("table table-bordered").append("<thead>").append("<tbody>").append("<tfoot>");
            var $thead = $table.children("thead");
            var $tbody = $table.children("tbody");
            var $tfoot = $table.children("tfoot");

            (function creHead() {
                $thead.append("<tr>");
                $thead.children("tr").append("<th>");

                for (var i = 0, l = data.length; i < l; i++) {
                    $("<th>").attr({
                        "colspan": data[i].data.length
                    }).addClass("text-center")
                        .text(data[i].name).appendTo($thead.children("tr"));
                }
            })();

            (function creBody() {
                $row1 = $("<tr>");
                $("<td>").addClass("text-center").text("制粉系统型式").appendTo($row1);

                $row2 = $("<tr>");

                $("<td>").addClass("text-center").html("漏风系数,K<sub>le</sub>").appendTo($row2);

                for (var i = 0, l = data.length; i < l; i++) {
                    for (var j = 0; j < data[i].data.length; j++) {
                        $("<td>").addClass("text-center").text(data[i].data[j].type).appendTo($row1);
                        $("<td>").addClass("text-center").text(data[i].data[j].K).appendTo($row2);
                    }
                }

                $tbody.append($row1).append($row2);
            })();

            (function creFoot() {
                var colspan = $tbody.find("tr:eq(0) > td").size();
                $tfoot.append("<tr>");
                $("<td>").attr({"colspan": colspan}).addClass("text-center")
                    .text("* 可按磨煤机直径D取值; D≥2m 取 0.4; D≥3m 取 0.3; D≥4m 取 0.2")
                    .appendTo($tfoot.children('tr'));
            })();

            return $("<div>").addClass("col-md-12").append($table).html();
        }

        $("#E61").popover({
            html: true,
            placement: "top",
            trigger: "focus",
            content: function () {
                return '<div class="col-md-12">' + getTable() + '</div>';
            }()
        })
    }

    return {
        init: function () {
            popoverHandle();
            $("#splitter_type").change(function () {
                var value = $(this).val();
                if (value == "1") {
                    $("#E2").val("1.0");
                } else {
                    $("#E2").val("1.2");
                }
            })
            $("#myglState").bind("click", function () {
                Choose.myglStateClick($(this))
            })
            $("#E12State").bind("click", function () {
                Choose.E12StateClick($(this))
            })
            $("#E74State").bind("click", function () {
                Choose.E74StateClick($(this))
            })
            $("#E75State").bind("click", function () {
                Choose.E75StateClick($(this))
            })
        },
        myglStateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("input[name=mygl]").attr("readonly", false)
                $("[name=myglLabel]").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("input[name=mygl]").val("")
                }
                $("input[name=mygl]").attr("readonly", true);
                $("[name=myglLabel]").removeClass("aTypeStyle");
            }
        },
        E12StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E12").attr("readonly", false)
                $("#E12Label").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("#E12").val("")
                }
                $("#E12").attr("readonly", true);
                $("#E12Label").removeClass("aTypeStyle");
            }
        },
        E74StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E74").attr("readonly", false)
                $("#E74Label").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("#E74").val("")
                }
                $("#E74").attr("readonly", true);
                $("#E74Label").removeClass("aTypeStyle");
            }
        },
        E75StateClick: function (obj, state) {
            //如果选中的情况
            if (obj.is(':checked')) {
                $("#E75").attr("readonly", false)
                $("#E75Label").addClass("aTypeStyle");
            } else {
                if (state == undefined) {
                    $("#E75").val("")
                }
                $("#E75").attr("readonly", true);
                $("#E75Label").removeClass("aTypeStyle");
            }
        },
        chooseSave: function () {
            if (!saveValidate())return;
            var addFlag = $("#addFlag").val();
            if (addFlag == "add" || (addFlag != "add" && confirm("确定重新计算并保存吗？"))) {
                Metronic.blockUI({
                    boxed: true,
                    message: "数据加载中..."
                });
                $.ajax({
                    url: basePath + "mill/select/chooseSave",
                    type: "post",
                    dataType: "json",
                    data: getChoose(),
                    success: function (data) {
                        Metronic.unblockUI();
                        if (data.ok) {
                            formSub(basePath + "mill/select?menuId=36&message=success")
                        } else {
                            toast.error(data.returnMsg);
                            return false;
                        }
                    }
                })
            }
        },
        chooseNext: function () {
            var addFlag = $("#addFlag").val();
            if (addFlag == "add") {
                toast.error("请先填写本页数据，保存成功后再进行下一页操作！");
                return;
            }
            formSub(basePath + "mill/calculate?menuId=37")
        }
    }
}();